<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<div id="app">
    <template>
        <el-form :model="queryForm" ref="queryForm" :inline="true">
            <el-form-item label="用户名" prop="userName">
                <el-input
                        v-model="queryForm.userName"
                        placeholder="请输入用户名"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="姓名" prop="realName">
                <el-input
                        v-model="queryForm.realName"
                        placeholder="请输入姓名"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>


            <el-form-item label="手机号码" prop="phone">
                <el-input
                        v-model="queryForm.phone"
                        placeholder="请输入手机号码"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>


            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-if="button.add"
                >
                    新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-if="button.export">
                    导出
                </el-button>
            </el-col>
        </el-row>

        <el-table
                v-loading="loading"
                element-loading-text="加载中"
                element-loading-spinner="el-icon-loading"
                :data="tableData"
                :header-cell-style="{'text-align':'center','color':'grey','font-weight':'600'}"
                :cell-style="{'text-align':'center'}" border style="margin-top: 10px;"
        >
            <el-table-column type="index" label="序号" width="100px"></el-table-column>
            <el-table-column prop="userName" label="用户名"></el-table-column>
            <el-table-column prop="realName" label="姓名"></el-table-column>
            <el-table-column prop="sex" label="性别" :formatter="formatSex"></el-table-column>
            <el-table-column prop="birthDate" label="出生日期"></el-table-column>
            <el-table-column prop="phone" label="手机号码"></el-table-column>
            <el-table-column prop="roleList" label="角色" :formatter="formatRole"></el-table-column>
            <el-table-column prop="postName" label="岗位">

                <template slot-scope="scope">
                    <span v-if="scope.row.postName" type="success" size="small">{{scope.row.postName}}</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button v-if="button.update"  size="mini" type="primary" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
                    </el-button>
                    <el-button v-if="button.delete" size="mini" type="info" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>


        <el-pagination
                background
                :page-sizes="[10, 20, 30, 40,50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :page-size.sync="pageSize"
                :current-page.sync="currentPage"
                @size-change="handleQuery"
                @current-change="handleQuery"
                style="float: right;margin-top: 10px"
        />
    </template>
</div>
</body>

<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/user.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>


<script>

    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: 1,
                pageSize: 10,
                total: 0,
                open: false,
                tableData: [],
                title: "",
                formData: {},
                button: {
                    add: false,
                    update: false,
                    delete: false,
                    imports: false,
                    export: false
                },
                rules: {
                    userName: [
                        {required: true, message: '请输入', trigger: 'blur'}
                    ],
                    realName: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    sex: [
                        {required: true, message: '请选择', trigger: 'blur'}
                    ],
                    birthDate: [
                        {required: true, message: '请选择', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入', trigger: 'blur'},
                        {min: 11, max: 11, message: '手机号码格式不正确', trigger: 'blur'}
                    ]
                },
                loading: false,
                roleList: [],
                postList: [],
                queryForm: {
                    userName: '',
                    realName: '',
                    phone: ''
                }
            }
        },
        created() {
            this.handleQuery();
            this.getRoleList()
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("sys/user/add") > -1;
            this.button.update = sysButtons.indexOf("sys/user/update") > -1;
            this.button.delete = sysButtons.indexOf("sys/user/delete") > -1;
            this.button.imports = sysButtons.indexOf("sys/user/imports") > -1;
            this.button.export = sysButtons.indexOf("sys/user/export") > -1;
        },
        methods: {
            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            getRoleList() {
                requests.post('/sys/role/all')
                    .then((res) => {
                        if (res.code === '200') {
                            this.roleList = res.data;
                        }
                    });
            },

            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },
            submitForm() {
                addOrUpdate('/sys/user/add', this);
            },
            cancel() {
                this.open = false
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            formatSex: function (row, column, cellValue, index) {
                return row.sex === 1 ? '男' : row.sex === 2 ? '女' : '未知';
            },
            formatRole: function (row, column, cellValue, index) {
                if (cellValue != null) {
                    let role = new Array();
                    cellValue.forEach(function (val, index, arr) {
                        role.push(val.roleName);
                    });
                    return role.toString();
                }
            },

            handleQuery() {
                let queryParam = {
                    pageNo: this.currentPage,
                    pageSize: this.pageSize,
                    userName: this.queryForm.userName,
                    realName: this.queryForm.realName,
                    phone: this.queryForm.phone
                };
                this.loading = true
                pageList(queryParam).then((res) => {
                    if (res.code === '200') {
                        this.loading = false
                        if (res.data.list) {
                            this.tableData = res.data.list;
                            this.total = Number(res.data.count);
                        } else {
                            this.tableData = res.data;
                        }
                    }
                });
            },
            handleDelete(row) {
                deleteById('/sys/user/delete/' + row.id, this);
            },
            handleAdd() {
                openPage('新增用户', '/system/sysUser/userAdd.html', this);

            },
            handleUpdate(row) {
                openPage('修改用户', '/system/sysUser/userUpdate.html?id=' + row.id, this);
            },
            handleImport() {
                openPage('导入用户', '/system/sysUser/userImport.html', this);
            },
            handleExport() {
                let queryParam = {
                    pageNo: this.currentPage,
                    pageSize: this.pageSize,
                    userName: this.queryForm.userName,
                    realName: this.queryForm.realName,
                    phone: this.queryForm.phone
                };
                exports('/sys/user/export', queryParam);
            }
        }
    });
</script>
<style>
</style>
</html>
